package com.dbh.alg.algorithm.hot100.栈;

import java.util.Deque;
import java.util.LinkedList;

public class Leetcode20_有效的括号 {

    public boolean isValid(String s) {
        if (s == null  || s.isEmpty()) {
            return true;
        }
        char[] charArray = s.toCharArray();
        Deque<Character> stack = new LinkedList<>();
        for (char c : charArray) {
            if (c == '(') {
                stack.push(')');
            } else if (c == '[') {
                stack.push(']');
            } else if (c == '{') {
                stack.push('}');
            } else {
                if (!stack.isEmpty()) {
                    Character pop = stack.pop();
                    if (pop != c) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }
}
